<?php defined('SYSPATH') or die('No direct script access.');

// URL: /<context>/index.php/Home/
class Controller_Home extends Controller_Base 
{
	private $postService;
	private $categoryService;
	
	public function __construct(Request $request, Response $response)
	{
		parent::__construct($request, $response);
		$this->postService = new Service_PostServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_POST.'/', ACCESS_TOKEN);
		$this->postService->init($this->_requestContext);
		
		$this->categoryService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_CATEGORY.'/', ACCESS_TOKEN);
		$this->categoryService->init($this->_requestContext);

	}
	
	public function action_index()
	{
		
		if ($this->_sessionInfo->userInfo['userSid'] > 0) {
			// Logged in:
			if ($this->_sessionInfo->currentProjectSid > 0) {
				// Go to the project page
				$redirTo = controller_base_path($this->_requestContext, CONTROLLERPAGE).'/Project';	
			} else if ($this->_sessionInfo->userInfo['userStatus'] < 2) {
				// Just Activated: show tutorial
				$redirTo = controller_base_path($this->_requestContext, CONTROLLERPAGE).'/'.$this->request->controller().'/intro';	
			} else {
				//
				$redirTo = CONTROLLERPAGE.'/'.$this->request->controller().'/stream';	
			}
		} else {
			// Not Logged in:
			$redirTo = CONTROLLERPAGE.'/Public';	
		}
		$this->request->redirect($redirTo);
	}
	
	public function action_intro()
	{
		$contentFrame = $this->index('Welcome to Seednia');
		$contentFrame->content = $this->createView('home_intro');
		try {
			$this->_projectHelper->loadUserProjects($this->template);
		} catch (Exception $ex)
		{
			Kohana_Log::instance()->add(Kohana_Log::ERROR, 'Exception::Add action_index: ' . $ex->getMessage());
		}
	}
	
	public function action_stream()
	{
		$contentFrame = $this->index('Network');
		$contentFrame->content = $this->createView('home_content');
		
		if ($this->_sessionInfo->userInfo['userSid'] > 0) {
			// Logged in
			//$this->template->scripts = array('public/js/home.js');
			
			try {
				$this->_projectHelper->loadUserProjects($this->template);
				
				$cateCriteria = array('kind' => 'Post');
				$ref_categories = $this->categoryService->getList($cateCriteria, 0, 10);
				$content->ref_categories = Util_Data::toMapptingArray($ref_categories, '@sid', 'name');
				
				$tagsCsv = $this->request->query('tagsCsv');
				if ($tagsCsv == null)
					$tagsCsv = $this->request->post('tagsCsv');
				$postCriteria = array('postTagNamesCsv'=>$tagsCsv, '_retrieveScope'=> 2);

				// Retrieve post of following users, projects
				$postCriteria['projectSid'] = 1;
				
				$content->posts = $this->postService->getPostList($postCriteria, 0, 15);
				$contentFrame->content = $content;

			} catch (Exception $ex)
			{
				Kohana_Log::instance()->add(Kohana_Log::ERROR, 'Exception::Add action_index: ' . $ex->getMessage());
			}
		} else {
			// Not logged in
			//$this->setTemplate('/tpl_plain');
			$content = View::factory($this->_theme.'/site_initial');
			$this->template->content = $content;
		}
	}
	
	public function toMapptingArray(&$dataSource, $keyPropName, $valPropName)
	{
		$retval = array();
		foreach ($dataSource as $entry)
		{
			$retval[$entry->{$keyPropName}] = $entry->{$valPropName};
		}
		return $retval;
	}
	
} // End Controller_Home